package com.wuseguang.springcloud.mina;

import com.wuseguang.springcloud.mina.changYing.AiCameraToCloudConstants;
import com.wuseguang.springcloud.mina.changYing.AiDataModel;
import com.wuseguang.springcloud.mina.changYing.ByteArrayUtils_BE;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

public class ByteModleDecoder extends CumulativeProtocolDecoder {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @Override
    public boolean doDecode(IoSession ioSession, IoBuffer in, ProtocolDecoderOutput out) throws Exception {

      /*  byte[] byten = new byte[in.limit()];
        in.get(byten, in.position(), in.limit());
        int length3 = ByteArrayUtils_BE.getInt_3(byten);
        System.err.println("0000000000000000000000长度："+length3);*/

        //单包数据小于12个字节,直接丢弃
        if (in.remaining() >= 12) {
            try {


                byte[] s = new byte[2];
                in.get(s);


                //读取协议版本
                byte[] version = new byte[1];
                in.get(version);
                //System.arraycopy(byten, 2, version, 0, 1);

                //读取数据长度
                byte[] dataLength = new byte[3];
                in.get(dataLength);
                //System.arraycopy(byten, 3, dataLength, 0, 3);
                int length = ByteArrayUtils_BE.getInt_3(dataLength);

                //读取指令
                byte[] order = new byte[2];
                in.get(order);
                //System.arraycopy(byten, 6, order, 0, 2);
                //读取mac
                byte[] mac = new byte[6];
                in.get(mac);
                //System.arraycopy(byten, 8, mac, 0, 6);

                //判断是不是心跳包,心跳包没有randId
                char aChar = ByteArrayUtils_BE.getChar(order);
                byte[] rand = new byte[0];
                byte[] data;

                if (aChar != AiCameraToCloudConstants.HEARTORDER) {
                    //非心跳
                    //读取随机数
                    rand = new byte[4];
                    in.get(rand);
                    //System.arraycopy(byten, 14, rand, 0, 4);
                    //获取data
                    data = new byte[length - 15];

                    //System.arraycopy(byten, 18, data, 0, length - 15);

              /* //获取crc校验码
               sum = new byte[2];
               System.arraycopy(byten, (18 + length - 15), sum, 0, 2);*/
                } else { //心跳
                    data = new byte[length - 11];
                    /* System.arraycopy(byten, 18, data, 0, length - 11);*/

               /*//获取crc校验码
               sum = new byte[2];*/
                    //System.arraycopy(byten, (18 + length - 11), sum, 0, 2);
                }

                in.get(data);
                //获取crc校验码
                byte[] sum = new byte[2];
                in.get(sum);
                //读包尾
                byte[] end = new byte[1];
                in.get(end);
                //System.arraycopy(byten, byten.length - 1, end, 0, 1);

                AiDataModel aiDataModel = new AiDataModel(version, order, mac, rand, data);
                out.write(aiDataModel);


                return false;
            } catch (Exception e) {
                logger.info("数据有误 ");
                AiDataModel aiDataModel = new AiDataModel();
                out.write(aiDataModel);
                return true;
            }
        }

        return true;
    }


}
